package com.ludomancy.archeologyrpg;

import java.awt.Point;
import java.util.Iterator;

import com.ludomancy.archeologyrpg.entities.AlienBase;
import com.ludomancy.archeologyrpg.entities.BuildSite;
import com.ludomancy.archeologyrpg.entities.Dwelling;
import com.ludomancy.archeologyrpg.entities.Fungus;
import com.ludomancy.archeologyrpg.entities.Generator;
import com.ludomancy.archeologyrpg.entities.Nest;
import com.ludomancy.archeologyrpg.entities.OxygenTank;
import com.ludomancy.archeologyrpg.entities.Rocket;
import com.ludomancy.archeologyrpg.entities.Ruin;
import com.ludomancy.archeologyrpg.entities.Swamp;
import com.ludomancy.gamelib.Randomizer;

public class LevelGenerator
{
	public static void generate(Game game)
	{
		// initial conditions
		game.rocket = new Rocket(new Point(72, 160 ));
		game.entities.add(game.rocket);
		AlienBase base = new AlienBase(getFreeRandomPosition());
		game.entities.add(base);
		Generator gen1 = new Generator(getFreeRandomPosition());
		game.entities.add(gen1);
		Generator gen2 = new Generator(getFreeRandomPosition());
		game.entities.add(gen2);
		Generator gen3 = new Generator(getFreeRandomPosition());
		game.entities.add(gen3);
		base.plugGenerator(gen1);
		base.plugGenerator(gen2);
		base.plugGenerator(gen3);
		game.entities.add(new Dwelling(getFreeRandomPosition()));
		game.entities.add(new BuildSite(getFreeRandomPosition()));
		Entity o2 = new OxygenTank(getFreeRandomPosition());
		game.entities.add(o2);
		game.entities.add(new Ruin(getFreeRandomPosition()));
		game.entities.add(new Ruin(getFreeRandomPosition()));
		game.entities.add(new Ruin(getFreeRandomPosition()));
		Nest nest = new Nest(getFreeRandomPosition());
		game.entities.add(nest);
		game.entities.add(new Fungus(getFreeRandomPosition(), nest));
		
		game.entities.add(new Swamp(getFreeRandomPosition()));
		
		for (Iterator iter = game.entities.iterator(); iter.hasNext();)
		{
			Entity entity = (Entity) iter.next();
			// by default, all are invisible until explored
			if(		entity != game.rocket && 
					!(entity instanceof Ruin) &&
					!(entity instanceof Fungus)
				)
			{
				entity.revelationTime = Game.discretize(Randomizer.getRandom(2000, 10000));
			}			
		}
	}
	
	public static Entity getRandomEntity()
	{
		boolean found = false;
		while(!found)
		{
			int i = Randomizer.getRandom(0, Game.game.entities.size()-1);
			return (Entity) Game.game.entities.elementAt(i);
		}
		return null;
	}
	
	public static Point getFreeRandomPosition()
	{
		Point p = new Point();
		boolean found = false;
		while(!found)
		{
			found = true;
			int x = Randomizer.getRandom(Game.ENTITY_TILE_SIZE, Game.LANDSCAPE_WIDTH  - Game.ENTITY_TILE_SIZE);
			int y = Randomizer.getRandom(Game.ENTITY_TILE_SIZE, Game.LANDSCAPE_HEIGHT - Game.ENTITY_TILE_SIZE);
			p = new Point(x, y);
			if(Game.game.getEntitiesInArea(p, Game.ENTITY_TILE_SIZE*2).size() > 0)
			{
				found = false;
				continue;
			}
		}
		return p;
	}
}
